// g++ -o float float.cc
#include <iostream>

#include "show.h"
using namespace std;
void Test1() {
  // 移位
  unsigned int u = 65;
  int s = -34;
  cout << (u << 2) << endl;
  cout << (u >> 3) << endl;  // 向下取整
  cout << (s << 2) << endl;
  cout << (s >> 3) << endl;  // 向下取整
}
void Test2() {
  // 扩展，扩充高位字节
  unsigned short int u1 = 1;
  show(u1);
  show((unsigned int)u1);  // 零扩展

  short int s1 = -1;
  show(s1);
  show((int)s1);  // 符号扩展

  // 截断,高位字节直接丢弃
  unsigned int u2 = 1;
  show(u2);
  show((unsigned short int)u2);

  int s2 = -1;
  show(s2);
  show((short int)s2);
}
void Test_Mod() {
  int a = -4;
  unsigned int b = 4;
  cout << a % 3 << endl;
  cout << b % 3 << endl;
}

bool endian_mode() {
  int a = 0x08;
  int* p = &a;
  if (*(char*)p == 0x08)
    cout << "little endian" << endl;
  else
    cout << "big endian" << endl;
  // show(a);
  return true;
}
int x_to_minus_x(int x) { return ~x + 1; }

// P60
unsigned replace_bytes(unsigned x, int i, unsigned char b) {
  unsigned mask = 0xff << (i << 3);
  return (x & ~mask) | ((unsigned)b << (i << 3));
}

// P62
bool int_shifts_are_arithmetic() {
  int test = -1;
  return !(test ^ (test >> 1));
}

// P64
int any_odd_one(unsigned x) {
  int mask = 0xAAAAAAAA;
  return !(x ^ mask);
}
// P10 原地交换
void inplace_swap(int* x, int* y) {
  *y = *x ^ *y;
  *x = *x ^ *y;
  *y = *x ^ *y;
}
// P11 对调数组头尾两端元素
void reverse_array(int a[], int cnt) {
  int first, last;
  for (first = 0, last = cnt - 1; first < last; ++first, --last)
    inplace_swap(&a[first], &a[last]);
}
// P12 数字==实现 !(x^y)

int main() {
  // show((float)0.125);  // 指定float类型，否则会认为是double类型
  // show((float)4.0);
  // show((float)-8.0);

  // float a = 3.14;
  // float b = 1e10;
  // cout << (a + b) - b << endl;  // 0
  // cout << a + (b - b) << endl;  // 3.14

  // Test1();
  // Test2();

  // int c = -4;
  // assert(-c == x_to_minus_x(c));
  // int d = 65;
  // assert(-d == x_to_minus_x(d));
  // endian_mode();

  // Test_Mod();
  // cout << (-1 % 26) << endl;

  // int a1 = 0x89ABCDEF;
  // int a2 = 0x76543210;
  // int a3 = (a1 & 0xff) | a2;
  // show(a3);

  // show(replace_bytes(0x12345678, 2, 0xAB));
  // show(replace_bytes(0x12345678, 0, 0xAB));
  // cout << int_shifts_are_arithmetic() << endl;

  // cout << any_odd_one(0xAAAAAAAA) << endl;
  // cout << 3 / 2 << endl;
  // cout << -3 / 2 << endl;

  // int8_t t1 = -100;
  // show(t1);
  // int8_t t2 = 2;  // 发生负溢出
  // int8_t ans = (int8_t)(t1 * t2);
  // printf("%d\n", ans);       // 56 ok
  // cout << (int)ans << endl;  // 8?
  // https://blog.csdn.net/qq_28256407/article/details/121004809
  int8_t a = 15;
  int8_t b = -6;
  int8_t c = a * b;
  show(a);
  show(b);
  show(c);
  printf("%d\n", c);
  return 0;
}